#define _CRT_SECURE_NO_WARNINGS 1
#include"seqlist.h"

void seqlistinit(sl* ps)
{
	ps->p = NULL;
	ps->cap = ps->size = 0;
}

void seqlistpushback(sl* ps, data x)
{
	if (ps->cap == ps->size)
	{
		int newcap = ps->cap == 0 ? 4 : ps->cap * 2;
		data* tmp = (data*)realloc(ps->p, newcap * sizeof(data));
		ps->p = tmp;
		ps->cap = newcap;
	}
	ps->p[ps->size] = x;
	ps->size++;
}

void seqlistpopback(sl* ps)
{
	if(ps->size!=0)
	ps->size--;

}

void seqlistpushfront(sl* ps, data x)
{
	if (ps->cap == ps->size)
	{
		int newcap = ps->cap == 0 ? 4 : ps->cap * 2;
		data* tmp = (data*)realloc(ps->p, newcap * sizeof(data));
		ps->p = tmp;
		ps->cap = newcap;
	}
	int m = ps->size-1;
	for(m;m<=0;m--)
	{
		ps->p[m + 1] = ps->p[m];
		
	}
	ps->p[0] = x;
	ps->size++;
}

int seqlistfind(sl* ps, data x)
{
	int y = 1;
	int m = ps->size - 1;
	int z = m+1;
	while(z)
	{
		if (ps->p[m] == x)break;
		else y = 0;
		m--; z--;
	}
	return m;
}

void seqlistprintf(sl* ps)
{
	int i = 0;
	while (i < ps->size)
	{
		printf("%2d", ps->p[i]);
		i++;
	}
}

void seqlistdetel(sl* ps, data x)
{
	int y = ps->size;
	while (x < y)
	{
		ps->p[x - 1] = ps->p[x];
		x++;
	}
	ps->size--;
}

void seqlistinsert(sl* ps, data x, data y)
{
	if (ps->cap == ps->size)
	{
		int newcap = ps->cap == 0 ? 4 : ps->cap * 2;
		data* tmp = (data*)realloc(ps->p, newcap * sizeof(data));
		ps->p = tmp;
		ps->cap = newcap;
	}
	int w = ps->size - 1;
	while (w >= x)
	{
		ps->p[w + 1] = ps->p[w];
		w--;
	}
	ps->p[x] = y;
	ps->size++;
}